<template>
  <PageWrapper>
    <a-card :bordered="false" class="pt-3 mb-3 proCard">
      <a-space align="center">
        <a-input-group style="width: 380px">
          <a-input
            :style="{ width: '100%' }"
            v-model="params.username"
            clearable
            placeholder="请输入账号"
            @keyup.enter="reloadTable"
          />
          <a-button type="primary" @click="reloadTable">
            <template #icon>
              <a-icon>
                <SearchOutlined />
              </a-icon>
            </template>
            查询
          </a-button>
        </a-input-group>
      </a-space>
    </a-card>
    <a-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        row-key="userId"
        ref="basicTableRef"
        :actionColumn="actionColumn"
        :row-selection="rowSelection"
        @select="handleSelect"
        @select-all="handleSelectAll"
      >
        <template #tableTitle>
          <a-space>
            <a-button type="primary" @click="openCreateDrawer">
              <template #icon>
                <PlusOutlined class="arco-icon" />
              </template>
              添加
            </a-button>
          </a-space>
        </template>
      </BasicTable>
    </a-card>

    <CreateDrawer
      ref="createDrawerRef"
      :title="drawerTitle"
      :roleList="roleData"
      @change="reloadTable"
    />
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { h, reactive, ref, onMounted } from 'vue';
  import { Message } from '@arco-design/web-vue';
  import { BasicTable, TableAction } from '@/components/Table';
  import { delUser, getUserList } from '@/api/system/user';
  import { columns } from './columns';
  import { PlusOutlined, SearchOutlined } from '@vicons/antd';
  import CreateDrawer from './CreateDrawer.vue';
  import { PageWrapper } from '@/components/Page';
  import { roleAllList } from '@/api/system/role';

  const basicTableRef = ref();
  const rowKeys = ref([]);
  const tableData = ref();
  const createDrawerRef = ref();
  const drawerTitle = ref('添加用户');
  const roleData = ref([]);

  const rowSelection = {
    type: 'checkbox',
    showCheckedAll: true,
  };

  function handleSelect(keys) {
    rowKeys.value = keys;
  }

  function handleSelectAll(checked) {
    console.log(`allChecked: ${checked}`);
    rowKeys.value = checked ? tableData.value.map((item) => item.id) : [];
  }

  const params = ref({
    username: '',
  });

  const actionColumn = reactive({
    width: 180,
    title: '操作',
    dataIndex: 'action',
    key: 'action',
    fixed: 'right',
    align: 'center',
    render({ record }) {
      return h(TableAction as any, {
        style: 'button',
        actions: [
          {
            label: '删除',
            popConfirm: {
              content: '您确定要删除吗？',
              okText: '确定',
              cancelText: '取消',
              onOk: handleDelete.bind(null, record),
            },
          },
          {
            label: '编辑',
            onClick: handleEdit.bind(null, record),
          },
        ],
      });
    },
  });

  function openCreateDrawer() {
    drawerTitle.value = '添加用户';
    const { openDrawer } = createDrawerRef.value;
    openDrawer();
  }

  const loadDataTable = async (res) => {
    const result = await getUserList({ ...params.value, ...res });
    tableData.value = result.list;
    return result;
  };

  function reloadTable() {
    basicTableRef.value.reload();
  }

  function handleEdit(record: Recordable) {
    console.log('点击了编辑', record);
    drawerTitle.value = '编辑用户';
    const { openDrawer } = createDrawerRef.value;
    openDrawer(record.userId);
  }

  function handleDelete(record: Recordable) {
    delUser({ userId: record.userId }).then(() => {
      Message.success('删除成功');
      reloadTable();
    });
  }

  onMounted(async () => {
    const res = await roleAllList();
    roleData.value = res.list.map((item: any) => {
      return {
        value: item.roleId,
        label: item.roleName,
      };
    });
  });
</script>

<style lang="less" scoped></style>
